package com.sunose.agfmk.utils;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;

import com.sunose.agfmk.constant.Constant;

/**
 * Created by William Wang on 13-8-31.
 */
public class GameAlertDialog extends DialogFragment{
    public final static String tag= Constant.TAG_PREFIX+"_GameAlertDialog";
    AlertDialog main=null;
    public static GameAlertDialog newInstance(String title) {
        GameAlertDialog frag = new GameAlertDialog();
        Bundle args = new Bundle();
        args.putString("title", title);
        frag.setArguments(args);
        frag.setStyle(STYLE_NO_FRAME, 0);
               return frag;
    }
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        String title = getArguments().getString("title");
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
        alertDialogBuilder.setTitle("Message!");
        alertDialogBuilder
                .setMessage(title)
                .setCancelable(false)//show modal model
                .setPositiveButton("Yes1",new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,int id) {
                        dialog.dismiss();
                        Log.d(tag, "alert at " + new java.util.Date());
                        removeDialog();

                    }
                })
                .setNegativeButton("No2",new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,int id) {
                        dialog.cancel();
                        Log.d(tag, "alert at " + new java.util.Date());
                        removeDialog();
                    }
                });

        // create alert dialog
        main=alertDialogBuilder.create();

        return main;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        Log.d(tag,"onDestroyView.....called");
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        super.onDismiss(dialog);
        Log.d(tag,"onDismiss.....called");
//        removeDialog();
    }
    public void removeDialog()
    {
       FragmentTransaction ft = this.getFragmentManager().beginTransaction();
//       Fragment prev = getFragmentManager().findFragmentByTag("dialog");
       ft.remove(this);
       ft.commit();
    }

    @Override
    public void onPause() {
        super.onPause();
        Log.d(tag,"onPause.....called");
    }
}
